package com.jia.lidao.ui.personal;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jia.lidao.R;
import com.jia.lidao.UserUtils;
import com.jia.lidao.enity.OrderlistRespDTO;
import com.jia.lidao.enity.RefreshStatusMessage;
import com.jia.lidao.network.http.BusinessManager;
import com.jia.lidao.network.http.MyCallback;
import com.jia.lidao.network.http.request.impl.OrderListRequest;
import com.jia.lidao.ui.BaseFragment;
import com.jia.lidao.ui.personal.adapter.OrderListAdapter;
import com.jia.lidao.utils.LogUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by jia on 17/12/16.
 */

public class ContentFragment extends BaseFragment {

    private PullToRefreshListView listView;

    private OrderListAdapter orderListAdapter;

    private static final String TYPE = "type";

    private static final String STATUS = "status";


    /**
     * 当前页码
     */
    private int mCurrentPage;


    public static ContentFragment newInstance(int type, int staus) {
        ContentFragment contentFragment = new ContentFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(TYPE, type);
        bundle.putInt(STATUS, staus);
        contentFragment.setArguments(bundle);
        return contentFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_content, null);
        LogUtil.printJ("onceateview");
        listView = (PullToRefreshListView) view.findViewById(R.id.lv_content_fragment);
        initData();
        autoScroll();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void initData() {
        orderListAdapter = new OrderListAdapter(getActivity(), getArguments().getInt(TYPE));
        listView.setAdapter(orderListAdapter);
        listView.setMode(PullToRefreshBase.Mode.BOTH);
        listView.setOnRefreshListener(
                new PullToRefreshBase.OnRefreshListener2<ListView>() {
                    @Override
                    public void onPullDownToRefresh(
                            PullToRefreshBase<ListView> refreshView) {
                        loadData(1);
                    }

                    @Override
                    public void onPullUpToRefresh(
                            PullToRefreshBase<ListView> refreshView) {
                        loadData(mCurrentPage + 1);
                    }
                });
    }


    /**
     * 加载数据
     *
     * @param page
     */
    private void loadData(final int page) {
        final OrderListRequest request = new OrderListRequest(page, getArguments().getInt(STATUS), getArguments().getInt(TYPE));
        BusinessManager.getInstance().getOrderList(request,
                new MyCallback<OrderlistRespDTO>() {
                    @Override
                    public void onSuccess(OrderlistRespDTO result,
                                          String message) {
                        if (result == null || result.orders == null || result.orders.data == null) {
                            showResultErrorToast();
                            return;
                        }
                        if (result.orders.data.size() == 0 && page != 1) {
                            showToast("没有更多数据");
                            return;
                        }
                        mCurrentPage = page;
                        if (page == 1) {
                            orderListAdapter.refreshUi(result.orders.data);
                        } else {
                            orderListAdapter.refreshUiByAdd(result.orders.data);
                        }
                    }

                    @Override
                    public void onError(String errorCode, String message) {
                        showToast(message);
                    }

                    @Override
                    public void onFinished() {
                        listView.onRefreshComplete();
                    }

                    @Override
                    public void onRefreshToken() {
                        UserUtils.getInstance().goToLogin(getActivity());
                    }
                });
    }

    /**
     * 自动加载
     */
    private void autoScroll() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                loadData(1);
            }
        }, 100);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RefreshStatusMessage message) {
        /* Do something */
        loadData(1);

    }
}
